package com.lgl.maker.generator.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.ArrayUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
 * @author ����
 * @verison FileGenerator.java.ftl.0
 */
public class StaticGenerator {
    public static void main(String[] args) throws IOException {


        String proPath = System.getProperty("user.dir");
//        String inputPath = proPath + File.separator + "sample_pro_all" + File.separator + "acm-template";
//        String outputPath = proPath;
//        copyFilesByHutool(inputPath, outputPath, false);

        File inputFile = new File("D:\\desk_pro\\md\\copy_i");
        File outputFile = new File("D:\\desk_pro\\md\\copy_o");


        StaticGenerator staticGenerator = new StaticGenerator();
        staticGenerator.mineCopy(inputFile, outputFile);


    }



    public static void copyFilesByHutool(String inputPath, String outputPath, boolean isOverride) {
        FileUtil.copy(inputPath, outputPath, isOverride);

    }

    public static void copyFileByRecursive(File inputFile, File outputFile) throws IOException {


        if (inputFile.isDirectory()) {
            File destOutputFile = new File(outputFile, inputFile.getName() + "hahaha");

            if (!destOutputFile.exists()) {
                destOutputFile.mkdir();
            }

            File[] files = inputFile.listFiles();

            if (ArrayUtil.isEmpty(files)) {
                return;
            }


            for (File file : files) {
                copyFileByRecursive(file, destOutputFile);
            }


        } else {
            Path p = outputFile.toPath().resolve(inputFile.getName());
            Files.copy(inputFile.toPath(), p, StandardCopyOption.REPLACE_EXISTING);
        }


    }


    public void mineCopy(File inputFile, File outputFile) throws IOException {

        //
        if (inputFile.isDirectory()) {
            //
            File destOutputFile = new File(outputFile, inputFile.getName() + "mineCopy");

            if (!destOutputFile.exists()) {
                destOutputFile.mkdir();
            } else {
                return;
            }


            File[] files = inputFile.listFiles();
            for (File file : files) {
                mineCopy(file, destOutputFile);
            }
        } else {
            Files.copy(inputFile.toPath(), outputFile.toPath().resolve(inputFile.getName() + "mineCopy"), StandardCopyOption.REPLACE_EXISTING);
        }
    }

}
